﻿import Mock from 'mockjs'
import cache from 'memory-cache'
import menuData from './menuData.json'

function getByPage(dataList,pageSize,pageNumber){
	var pageData= _.chunk(dataList,pageSize)
	return pageData[pageNumber-1];
}

//表格数据模拟
Mock.mock(/table-test.json/,(requestParam)=>{
	var params=JSON.parse(requestParam.body);
	console.log(params);
	var listAll=null;
	var data={
		status:"success",
		info:"查询成功",
		payload:{}
	};
	if(!(listAll=cache.get('table-test'))){
		listAll= Mock.mock({
		  "array|100":[{
				"name":"@cname()",
				"qq":"@natural(12)",
				"phone":'@natural(11)',
				"updateTime":'@datetime("yyyy-M-d H:m:s")'
		  }]
		}).array;
		cache.put('table-test', listAll);
	}
	requestParam=JSON.parse(requestParam.body);
	function valuetest(filter,item){
		var test=false;
		switch(filter.compare){
			case "eq"://等于
				test=filter.value==item[filter.prop];
				break;
			case "gt"://大于
				break;
			case "lt"://小于
				break;
			case "lte"://小于等于
				break;
			case "gte"://大于等于
				break;
			case "like"://包含
				test=filter.value.indexOf(item[filter.prop])>-1;
				break;
			default:
				test=false;
				break;
		}
		return test;
	}
	if(requestParam.hasOwnProperty("filters")){
		listAll=_.filter(listAll, function(item) {
			var test=true;
			for(let i=0;i<requestParam.filters.length;i++){
				var filter=requestParam.filters[i];
				test=test && valuetest(filter,item);
			}
			return test;
		});
	}
	var dataPage = getByPage(listAll,params.pageSize,params.pageNumber);
	data.payload={total:listAll.length,list:dataPage};
	return data;
})
//导航菜单
Mock.mock(/menuData/,menuData);


